﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class Payment : TAM_SPORT.FormBase
    {
        private PaymentDao payDao;
        public Payment()
        {
            InitializeComponent();
            lblPayPerson.Text = LoginDao.fullName;
            payDao = new PaymentDao();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (check())
            {
                float total = 0;
                if (float.TryParse(txtTotal.Text.Trim(), out total))
                {
                    if (payDao.insertPayment(txtReason.Text.Trim(), txtReceivePerson.Text.Trim(), txtNotes.Text.Trim(), dtPayDate.Value, total, LoginDao.userID))
                    {
                        Result r = new Result(UserConstant.SAVE_SUCCESSFUL);
                        r.ShowDialog();
                        r.Dispose();
                        txtNotes.Text = "";
                        txtReason.Text = "";
                        txtTotal.Text = "";
                        txtReceivePerson.Text = "";
                        dtPayDate.Value = DateTime.Now;
                    }
                    else
                    {
                        Error er = new Error(UserConstant.SAVE_FAIL);
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
                else
                {
                    Error er = new Error(UserConstant.INPUT_VALIDATE_WRO);
                    er.ShowDialog();
                    er.Dispose();
                }
            }
            else
            {
                Error er = new Error(UserConstant.INPUT_VALIDATE);
                er.ShowDialog();
                er.Dispose();
            }
        }

        private bool check()
        {
            if (txtReason.Text.Trim() == ""
                || txtReceivePerson.Text.Trim() == ""
                || txtTotal.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }

        private void txtTotal_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private String RemoveComma(String value)
        {
            if (value != null && value.Contains(","))
            {
                value = value.Replace(",", String.Empty).Trim();
            }
            return value;
        }

        private void txtTotal_TextChanged(object sender, EventArgs e)
        {
            if (RemoveComma(txtTotal.Text).Length > 3)
            {
                txtTotal.Text = decimal.Parse(RemoveComma(txtTotal.Text)).ToString("#,###");
                txtTotal.Select(txtTotal.Text.Length, 0);
            }
        }
    }
}
